﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Reflection;
using eCommerce.db.DataRespository;
using eCommerce.db.SQLDataAccess;

namespace eCommerce.db.DALRespository
{
    public class CommentDal
    {
        #region CommentDal private
        private static void SetInfo(IDataRecord reader, ref CommentEntity o)
        {
            for (int i = 0; i < reader.FieldCount; i++)
            {
                PropertyInfo propertyInfo = DbCommon.GetPropertyInfo(o, reader.GetName(i));
                if (((propertyInfo != null) && (reader[i] != null)) && (reader[i] != DBNull.Value))
                    propertyInfo.SetValue(o, reader[i], null);
                else
                    o[reader.GetName(i)] = reader[i];
            }
        }

        private static void SetInfoList(ref SqlDataReader reader, ref List<CommentEntity> commentList)
        {
            while (reader.Read())
            {
                var o = new CommentEntity();
                SetInfo(reader, ref o);
                commentList.Add(o);
            }
            reader.Dispose();
        }
        #endregion

        public static int Create(CommentEntity o)
        {
            DbConnection.SetConnection(DbConnection.ConnectionName.Main);
            try
            {
                return (int)SqlHelper.ExecuteScalar(DbCommon.ConnectionString, DbCommon.DatabaseSchema + "Comment_Create", o.Id, o.ParentId, o.TypeId, o.Status, o.UserName, o.Email, o.Avatar, o.Body);
            }
            catch (Exception ex)
            {
                throw new Exception(string.Format(DbCommon.DatabaseSchema + "Comment_Create: {0}", ex.Message));
            }
        }

        public static void UpdateStatus(int commentId, int status)
        {
            DbConnection.SetConnection(DbConnection.ConnectionName.Main);
            try
            {
                SqlHelper.ExecuteNonQuery(DbCommon.ConnectionString, "Comment_Update_Status", commentId, status);
            }
            catch (Exception ex)
            {
                throw new Exception(string.Format(DbCommon.DatabaseSchema + "Comment_Update_Status: {0}", ex.Message));
            }   
        }

        public static void Delete(int commentId)
        {
            DbConnection.SetConnection(DbConnection.ConnectionName.Main);
            try
            {
                SqlHelper.ExecuteScalar(DbCommon.ConnectionString, "Comment_Delete", commentId);
            }
            catch (Exception ex)
            {
                throw new Exception(string.Format(DbCommon.DatabaseSchema + "Comment_Delete: {0}", ex.Message));
            }    
        }
       
        public static int GetStatistic(int status)
        {
            DbConnection.SetConnection(DbConnection.ConnectionName.Main);
            try
            {
                return (int)SqlHelper.ExecuteScalar(DbCommon.ConnectionString, "Comment_GetStatistic", status);
            }
            catch (Exception ex)
            {
                throw new Exception(string.Format(DbCommon.DatabaseSchema + "Comment_GetStatistic: {0}", ex.Message));
            }    
        }

        public static List<CommentEntity> GetByParentId(int id, int parentId, int status)
        {
            DbConnection.SetConnection(DbConnection.ConnectionName.Main);
            var lstComment = new List<CommentEntity>();
            try
            {
                var reader = SqlHelper.ExecuteReader(DbCommon.ConnectionString, DbCommon.DatabaseSchema + "Comment_GetByParentId", id, parentId, status);

                SetInfoList(ref reader, ref lstComment);

                return lstComment;
            }
            catch (Exception ex)
            {
                throw new Exception(string.Format(DbCommon.DatabaseSchema + "Comment_GetByParentId: {0}", ex.Message));
            }
        }  

        public static CommentEntity GetById(int id)
        {
            DbConnection.SetConnection(DbConnection.ConnectionName.Main);
            var comment = new CommentEntity();
            try
            {
                var reader = SqlHelper.ExecuteReader(DbCommon.ConnectionString, DbCommon.DatabaseSchema + "Comment_GetById", id);
                if (reader.Read())
                    SetInfo(reader, ref comment);
                return comment;
            }
            catch (Exception ex)
            {
                throw new Exception(string.Format(DbCommon.DatabaseSchema + "Comment_GetById: {0}", ex.Message));
            }
        }        
    }
}
